﻿using AntMovement.Core.Enum;
using AntMovement.Parameter;
using MaterialDesignThemes.Wpf;
using Prism.Commands;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AntMovement.Menu.Device.ViewModels.Dialog
{
    public class IOAddDialogModel
    {
        public IEnumerable<MotionType> MotionTypes 
        { 
            get 
            {
                foreach (MotionType c in (MotionType[])Enum.GetValues(typeof(MotionType)))
                {
                    yield return c;
                }
            } 
        }
        public MotionType Type { get; set; } = MotionType.SolidTech_E_SerialCard;
        public string MotionName { get; set; }
        public int CardNo { get; set; }
        public int IONumber { get; set; } = 16;
        public string IP { get; set; }
        public string IOType { get; set; } = "输入/输出";
        private IOParameter _iOParameter;
        public IOAddDialogModel(IOParameter iOParameter)
        {
            _iOParameter = iOParameter;
        }

        public DelegateCommand Confirm => new DelegateCommand(() =>
        {
            int count = IONumber / 16;
            for (int i = 0; i < count; i++) 
            {
                for (int j = 0; j < 16; j++)
                {
                    
                    switch (IOType)
                    {
                        case "输入":
                            _iOParameter.DIDataSet.Add(new Core.Entity.DI()
                            {
                                IP = IP,
                                Id = Guid.NewGuid(),
                                Alarm = new Core.Entity.Alarm(),
                                CardNo = CardNo,
                                Index = _iOParameter.DIDataSet.Count + 1,
                                MdlIndex = i,
                                OrderlyNum = j,
                                MotionName = MotionName,
                                Type = Type,
                            });
                            break;
                        case "输出":
                            _iOParameter.DODataSet.Add(new Core.Entity.DO()
                            {
                                IP = IP,
                                Id = Guid.NewGuid(),
                                CardNo = CardNo,
                                Index = _iOParameter.DODataSet.Count + 1,
                                MdlIndex = i,
                                OrderlyNum = j,
                                MotionName = MotionName,
                                Type = Type,
                            });
                            break;
                        case "输入/输出":
                            _iOParameter.DIDataSet.Add(new Core.Entity.DI()
                            {
                                IP = IP,
                                Id = Guid.NewGuid(),
                                Alarm = new Core.Entity.Alarm(),
                                CardNo = CardNo,
                                Index = _iOParameter.DIDataSet.Count + 1,
                                MdlIndex = i,
                                OrderlyNum = j,
                                MotionName = MotionName,
                                Type = Type,
                            });
                            _iOParameter.DODataSet.Add(new Core.Entity.DO()
                            {
                                IP = IP,
                                Id = Guid.NewGuid(),
                                CardNo = CardNo,
                                Index = _iOParameter.DODataSet.Count + 1,
                                MdlIndex = i,
                                OrderlyNum = j,
                                MotionName = MotionName,
                                Type = Type,
                            });
                            break;
                    }
                }
            }
            DialogHost.Close(PopUpType.Global.ToString());
        });
    }
}
